package com.loe.logger

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.loe.logger.util.LoggerTools
import com.loe.logger.viewpager.LoggerFragTextItem
import kotlinx.android.synthetic.main.logger_activity.*

class LoggerActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        // 状态栏透明 >=6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = LoggerTools.mainColor
        }
        setContentView(R.layout.logger_activity)

        initView()
    }

    private fun initView()
    {
        viewServeEnvironment.visibility = if(LoeLogger.staticServeEnvironmentList.isNotEmpty()) View.VISIBLE else View.GONE
        textServeEnvironment.text = LoeLogger.environmentServeType

        viewWebEnvironment.visibility = if(LoeLogger.staticWebEnvironmentList.isNotEmpty()) View.VISIBLE else View.GONE
        textWebEnvironment.text = LoeLogger.environmentWebType

        buttonClear.setOnClickListener()
        {
            when(viewPager.index)
            {
                0 ->
                {
                    LoeLogger.clearNet()
                    (viewPager.nowFragment as LoggerNetFragment).loadData()
                }
                1 ->
                {
                    LoeLogger.clear()
                    (viewPager.nowFragment as LoggerLogFragment).loadData()
                }
            }
        }
        buttonBack.setOnClickListener()
        {
            finish()
        }

        val serveEnvironmentDialog = LoggerServeEnvironmentDialog(this)
        val webEnvironmentDialog = LoggerWebEnvironmentDialog(this)

        viewServeEnvironment.setOnClickListener()
        {
            serveEnvironmentDialog.show()
            {
                LoeLogger.environmentServeType = it.type
                LoeLogger.environmentServeBaseUrl = it.baseUrl
                textServeEnvironment.text = it.type
                LoeLogger.onServeSelect?.invoke(it)

                if(LoeLogger.environmentWebType == "自动环境")
                {
                    LoeLogger.onWebSelect?.invoke(EnvironmentBean(LoeLogger.environmentWebType, LoeLogger.environmentServeBaseUrl))
                }
            }
        }

        viewWebEnvironment.setOnClickListener()
        {
            webEnvironmentDialog.show()
            {
                LoeLogger.environmentWebType = it.type
                LoeLogger.environmentWebBaseUrl = it.baseUrl
                textWebEnvironment.text = it.type
                if(it.type == "自动环境")
                {
                    LoeLogger.onWebSelect?.invoke(EnvironmentBean(it.type, LoeLogger.environmentServeBaseUrl))
                }else
                {
                    LoeLogger.onWebSelect?.invoke(it)
                }
            }
        }

        viewPager.init(supportFragmentManager, viewLine, 0.5,
            LoggerFragTextItem(LoggerNetFragment::class.java, menu0, LoggerTools.textColor, LoggerTools.mainColor),
            LoggerFragTextItem(LoggerLogFragment::class.java, menu1, LoggerTools.textColor, LoggerTools.mainColor),
            LoggerFragTextItem(LoggerTestFragment::class.java, menu2, LoggerTools.textColor, LoggerTools.mainColor)
        )
    }
}